package com.mapaower.admin.controller;

import com.mapaower.admin.entity.Role;
import com.mapaower.admin.service.RoleService;
import com.mapaower.admin.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/admin/roles")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 获取所有角色列表
     */
    @GetMapping
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<ResponseResult<?>> getAllRoles() {
        List<Role> roles = roleService.getAllRoles();
        return ResponseEntity.ok(ResponseResult.success(roles));
    }

    /**
     * 根据ID获取角色信息
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<ResponseResult<?>> getRoleById(@PathVariable Long id) {
        Role role = roleService.getRoleById(id);
        if (role != null) {
            return ResponseEntity.ok(ResponseResult.success(role));
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 创建新角色
     */
    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<ResponseResult<?>> createRole(@RequestBody Role role) {
        try {
            Role createdRole = roleService.createRole(role);
            return ResponseEntity.ok(ResponseResult.success(createdRole));
        } catch (RuntimeException e) {
            return ResponseEntity.badRequest().body(ResponseResult.error(e.getMessage()));
        }
    }

    /**
     * 更新角色信息
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<ResponseResult<?>> updateRole(@PathVariable Long id, @RequestBody Role role) {
        try {
            Role updatedRole = roleService.updateRole(id, role);
            return ResponseEntity.ok(ResponseResult.success(updatedRole));
        } catch (RuntimeException e) {
            return ResponseEntity.badRequest().body(ResponseResult.error(e.getMessage()));
        }
    }

    /**
     * 删除角色
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<ResponseResult<?>> deleteRole(@PathVariable Long id) {
        boolean result = roleService.deleteRole(id);
        if (result) {
            return ResponseEntity.ok(ResponseResult.success("角色已删除"));
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 为角色添加权限
     */
    @PostMapping("/{roleId}/permissions/{permissionId}")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<ResponseResult<?>> addPermissionToRole(@PathVariable Long roleId, @PathVariable Long permissionId) {
        try {
            Role role = roleService.addPermissionToRole(roleId, permissionId);
            return ResponseEntity.ok(ResponseResult.success(role));
        } catch (RuntimeException e) {
            return ResponseEntity.badRequest().body(ResponseResult.error(e.getMessage()));
        }
    }

    /**
     * 从角色中移除权限
     */
    @DeleteMapping("/{roleId}/permissions/{permissionId}")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<ResponseResult<?>> removePermissionFromRole(@PathVariable Long roleId, @PathVariable Long permissionId) {
        try {
            Role role = roleService.removePermissionFromRole(roleId, permissionId);
            return ResponseEntity.ok(ResponseResult.success(role));
        } catch (RuntimeException e) {
            return ResponseEntity.badRequest().body(ResponseResult.error(e.getMessage()));
        }
    }
}